$(function () {
    search();
    $("#error").hide();
    $("#error2").hide();
    $("#error3").hide();
});

//回车键事件
var a = 1;
document.onkeydown = function (event) {
    if (a == 1) {
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if (e && e.keyCode == 13) {
            search();
        }
    }
};

//毫秒值格式化为时间格式
Date.prototype.toLocaleString = function() {
    return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate();
};

function edit() {
    var id = $("#userid").val();
 //   alert(id);
    var truename = $("#truename").val();
    var mail = $("#mail").val();
    var role = $("#role").val();
    var email=/^([a-zA-Z0-9_\.-]+)@(([a-zA-Z0-9_-]+)\.)+[a-zA-Z]{2,3}$/;
    if(email.test(mail) && truename != null && truename != ""){
        $.ajax({
            type:"POST",
            url:"modifyuser",
            data:{truename:truename, mail:mail, role:role, userId:id},
            dataType:"json",
            success:function (data) {
                if(data === true){
                    location.href = "userLists";
                }
                else {
                    layer.alert("修改信息失败");
                }
            },
            error:function () {
                layer.alert("修改信息失败");
            }
        });
    }
    else{
        if(truename == null || truename == ""){
            $("#error2").show();
        }
        if(mail == null || mail == ""){
            $("#error3").show();
        }
        if(!email.test(mail) && mail != ""){
            $("#error").show();
        }
    }
}

function del() {
    var id = $("#userid").val();
 //   alert(id);
    $.ajax({
        type:"POST",
        url:"deleteUser",
        data:{userid:id},
        dataType:"json",
        success:function(data){
            if(data === true){
             //   layer.alert("操作成功");
                location.href = "userLists";
            }
            if(data === false){
                layer.alert("删除用户失败");
            }
        },
        error:function () {
            layer.alert("删除用户失败");
        }
    });
}

//获取当前用户的id,重新点击编辑按钮时，信息隐藏
function getid(id) {
  //  alert(id);
    $("#userid").val(id);
    $("#userid2").val(id);
    $("#error").hide();
    $("#error2").hide();
    $("#error3").hide();
    $("#truename").val("");
    $("#mail").val("");
}

//搜索按钮
function search() {
    //   alert("search");
    var username = $("#username").val();
    var name = $("#name").val();
    var roleid = $("#roleId").val();
    $.ajax({
        type: "POST",
        url: "List",
        data: {username: username, name: name, roleId: roleid},
        dataType: "json",
        success: function (data) {
            console.log(data.length);
            var str = "";
            if (data.length > 0) {
                jQuery.each(data, function (i, item) {
                    //    alert(item.userName + "," + item.tureName + "," + item.roleName +
                    // "," + item.createTime + "," + item.mail);
                //    $("#aaa").val(item.userId);
                    var index = i + 1;
                    var unixTimestamp = new Date( item.createTime ) ;
                    createTime = unixTimestamp.toLocaleString();
                    str += "<tr class=\"warning\">";
                    str += "<td>" + index + "</td>";
                    str += "<td>" + item.userName + "</td>";
                    str += "<td>" + item.tureName + "</td>";
                    str += "<td>" + item.roleName + "</td>";
                    str += "<td>" + createTime + "</td>";
                    str += "<td>" + item.mail + "</td>";
                    str += "<td><a href=\"#myModal\" onclick='getid(" + item.userId + ")' data-toggle=\"modal\" class=\"btn btn-info\">编辑</a>";
                    str += "&nbsp;&nbsp;<a href=\"#myModal2\" onclick='getid(" + item.userId + ")' data-toggle=\"modal\" class=\"btn btn-danger\">删除</a></td>";
                    str += "</tr>";
                    //   alert(str);
                    $("#list").html(str);
                });
            }
            else {
                $("#list").html("");
            }
            //执行分页函数,
            $(function () {
                //设置页数按钮所在的层,
                $("div.holder").jPages({
                    //显示数据所在的块的ID
                    containerID: "list",
                    first: '首页',
                    last: '尾页',
                    previous: '上页',
                    next: '下页',
                    //每页显示数据为多少行
                    perPage: 5,
                    //起始页
                    startPage: 1,
                    //开始页码为2个
                    startRange: 1,
                    //最多显示几个页码页码,其余用..代替
                    midRange: 4,
                    //结束页码为1个
                    endRange: 1,
                    //数据显示的动画效果
                    animation: 'wobble',
                    keyBrowse: true
                });
            });
        },
        error: function (XMLHttpRequest, textStatus) {
            /*alert(XMLHttpRequest.status);
             alert(XMLHttpRequest.readyState);
             alert(textStatus);*/
            layer.alert("数据读取错误!");
        }
    });
}